﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeForing.Common;

namespace TimeForing.Models
{
    public class TimeforingRepository
    {
        private TimeforingDataContext db = new TimeforingDataContext();

        #region "Project"

        public IQueryable<Project> FindAllProjects()
        {
            return db.Projects;
        }

        public IEnumerable<SelectListItem> GetProjectList()
        {
            IEnumerable<SelectListItem> selectList = from project in db.Projects
                                                     select new SelectListItem { Value = project.ProjectID.ToString(), Text = project.ProjectID.ToString() + "/" + project.Name };

            //var list = new SelectList(items,);


            return selectList;
        }

        public Project GetProject(int id)
        {
            return db.Projects.SingleOrDefault(d => d.ProjectID == id);
        }

        public void AddProject(Project project)
        {
            db.Projects.InsertOnSubmit(project);
        }
        #endregion

        #region "Code"

        public IQueryable<Code> FindAllCodes()
        {
            return db.Codes;
        }

        public Code GetCode(int id)
        {
            return db.Codes.SingleOrDefault(d => d.CodeID == id);
        }


        public void AddCode(Code c)
        {
            db.Codes.InsertOnSubmit(c);
        }

        public List<SelectListItem> GetCodesForTask(Task t)
        {
            var items = from code in db.Codes
                        select code;

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in items)
            {
                SelectListItem i = new SelectListItem
                {
                    Value = item.CodeID.ToString(),
                    Text = item.ProjectID.ToString() + "/" + item.CodeNumber.ToString() + " - " + item.Name,
                    Selected = t.CodeID == item.CodeID
                };
                list.Add(i);
            }
            return list;

        }


        #endregion

        #region "User"

        public IQueryable<User> FindAllUsers()
        {
            return db.Users;
        }

        public User GetUser(int id)
        {
            return db.Users.SingleOrDefault(d => d.UserID == id);
        }


        public void AddUser(User u)
        {
            db.Users.InsertOnSubmit(u);
        }

        public SelectList GetUsersList()
        {
            var items = from user in db.Users
                        select new { user.UserID, user.Name };
            var list = new SelectList(items, "UserID", "Name");
            return list;
        }
        public List<SelectListItem> GetUsersForTask(int id)
        {
            var items = from user in db.Users
                        select user;
            Task currTask = this.GetTask(id);
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in items)
            {
                SelectListItem i;
                if (currTask != null)
                    i = new SelectListItem { Value = item.UserID.ToString(), Text = item.Name, Selected = currTask.UserID == item.UserID };
                else
                    i = new SelectListItem { Value = item.UserID.ToString(), Text = item.Name };
                list.Add(i);
            }
            return list;
        }

        #endregion

        #region "Sprint"

        public IQueryable<Sprint> FindAllSprints()
        {
            return db.Sprints;
        }

        public Sprint GetSprint(int id)
        {
            return db.Sprints.SingleOrDefault(d => d.SprintID == id);
        }

        public void AddSprint(Sprint s)
        {
            db.Sprints.InsertOnSubmit(s);
        }

        public SelectList GetSprintList()
        {
            var items = from sprint in db.Sprints
                        select new { sprint.SprintID, sprint.Name };
            var list = new SelectList(items, "SprintID", "Name");
            return list;
        }

        public List<SelectListItem> GetSprintListFromTask(Task t)
        {
            var items = from sprint in db.Sprints
                        select new { sprint.SprintID, sprint.Name };

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in items)
            {
                SelectListItem i = new SelectListItem { Value = item.SprintID.ToString(), Text = item.Name, Selected = t.SprintID == item.SprintID };
                list.Add(i);
            }
            return list;
        }

        public List<Task> GetTasksInSprint(int sprintId)
        {
            var items = from task in db.Tasks
                        where task.SprintID == sprintId
                        select task;
            List<Task> tasks = new List<Task>();
            foreach (var item in items)
            {
                tasks.Add(item);
            }

            return tasks;

        }

        #endregion

        #region "Tasks"

        public IQueryable<Task> FindAllTasks()
        {
            return db.Tasks;
        }

        public List<SelectListItem> GetSprintsForTask(int id)
        {
            var items = from sprint in db.Sprints
                        select sprint;
            Task currTask = this.GetTask(id);
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in items)
            {
                SelectListItem i = new SelectListItem { Value = item.SprintID.ToString(), Text = item.Name, Selected = currTask.SprintID == item.SprintID };
                list.Add(i);
            }
            return list;
        }

        public Task GetTask(int id)
        {
            return db.Tasks.SingleOrDefault(d => d.TaskID == id);
        }

        public void AddTask(Task t)
        {
            db.Tasks.InsertOnSubmit(t);
        }

        public List<Task> GetUserTasksForWeek(User user, int week, int year)
        {
            DateTime startDate = Util.StartDateInWeek(week, year);
            DateTime endDate = startDate.AddDays(6);


            var activeSprints = from s in db.Sprints
                                where s.StartDate <= startDate && s.EndDate >= endDate
                                select s.SprintID;
            var allUserTasks = from t in db.Tasks
                               where t.UserID == user.UserID
                               select t;

            var userTasks = allUserTasks
                            .Where(t => activeSprints.Contains((int) t.SprintID))
                            .Select(t => t)
                            .ToList();
            return userTasks;
        }


        #endregion

        #region "HourRegs"
        public void AddHourReg(HourReg h)
        {
            db.HourRegs.InsertOnSubmit(h);
        }

        public IQueryable<HourReg> GetHourRegs()
        {
            return db.HourRegs;
        }


        #endregion

        #region "Stories"

        public IQueryable<Story> FindAllStories()
        {
            return db.Stories;
        }

        public Story GetStory(int id)
        {
            return db.Stories.SingleOrDefault(d => d.StoryID == id);
        }

        public void AddStory(Story s)
        {
            db.Stories.InsertOnSubmit(s);
        }

        public List<SelectListItem> GetStorysForTask(int id)
        {
            var items = from s in db.Stories
                        select s;
            Task currTask = this.GetTask(id);
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in items)
            {
                SelectListItem i;
                if (currTask != null)
                    i = new SelectListItem { Value = item.StoryID.ToString(), Text = item.Name, Selected = currTask.StoryID == item.StoryID };
                else
                    i = new SelectListItem { Value = item.StoryID.ToString(), Text = item.Name, Selected = false };
                list.Add(i);
            }
            return list;
        }


        #endregion


        public void Save()
        {
            db.SubmitChanges();
        }
    }
}
